package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
/**
 * JDBC java数据库链接
 */
public class JDBCDemo1 {
    public static void main(String[] args) throws Exception {
        /*
        加载驱动
        加载MySQL的驱动
         */
        Class.forName("com.mysql.cj.jdbc.Driver");
        //使用驱动管理器
        System.out.println("正在链接数据库...");
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root");
        System.out.println("链接数据库成功!");
        Statement statement = connection.createStatement();
        /*
        创建一张名为user2的表
         */
        String sql="CREATE TABLE user2(\n" +
                "id INT PRIMARY KEY AUTO_INCREMENT,\n" +
                "username VARCHAR(30) NOT NULL,\n" +
                "password VARCHAR(30) NOT NULL,\n" +
                "nickname VARCHAR(30) NOT NULL,\n" +
                "age INT(3)\n" +
                ")";
        System.out.println(sql);
        statement.execute(sql);
        System.out.println("表创建成功");
        connection.close();



    }
}
